﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using University.Workshop.Business;

namespace University.Workshop.Presenters.Kardex.Register
{
    public class PresenterSecondStepForRegisterKardex
    {

        ISecondStepForRegisterKardex view;
        public PresenterSecondStepForRegisterKardex(ISecondStepForRegisterKardex view)
        {
            this.view = view;
        }

        public void UpdateData()
        {
            view.PersonId = view.KardexProductToRegister.Person.PersonId;
            if(view.KardexProductToRegister.AllotmentModel!=null)
                if (view.KardexProductToRegister.Allotment.Count != 0)
                {
                    RefreshSquare();
                    view.SquareSelected = ManagerSquare.GetSquareById(new Model.Square{SquareId = view.KardexProductToRegister.Allotment.First().SquareId});
                    SelectedSquare();
                    view.AllotmentName = view.KardexProductToRegister.Allotment.First().AllotmentName;
                    view.EnableAllotmentName = true;
                }
        }

        public void ChargeDataMedicineProduct()
        {
            if(view.SquareSelected!=null)
            view.KardexProductToRegister.Allotment = new List<Model.Allotment>{ 
                new Model.Allotment()
            {
                AllotmentName=view.AllotmentName,
                SquareId=view.SquareSelected.SquareId,
            }
            };
        }

        public void RefreshSquare()
        {
            List<Model.Square> squares = ManagerSquare.GetAllSquaresWithAllotmentsNotAssignedAndEnables();
            view.setListSquaresAvailable(squares);
        }

        public void SelectedSquare()
        {
            if(view.SquareSelected!=null)
            {
                ICollection<Model.Allotment> q = ManagerAllotment.GetAllotmentNotAssignedInSquare(view.SquareSelected );
                if (q.Count != 0)
                {
                    view.EnableAllotmentName = true;
                    view.setListAllotmentName(q);
                }
                else
                {
                    view.SquareSelected = null;
                    view.ErrorMessage="El Manzano seleccionado no tiene lotes sin asignar porfavor seleccione otro Manzano";
                }
            }
        }
    }
}
